﻿Public Class ConexionDesdeAPP 
    Public Sub con()

        TextBox7.Text = String.Format("User Id={0};Password={1};Host={2};Database={3};Persist Security Info=True;Schema={4}", usuario.Text, contraseña.Text, host.Text, base.Text, schema.Text)
        cadenaMascara.Text = ConexionSGS.ObtenerCadenaDeConexionConContrasenaOculta(TextBox7.Text)
    End Sub
    Public Sub defa()
        host.Text = My.Resources.ConexionSGS.host
        puerto.Text = My.Resources.ConexionSGS.puerto
        schema.Text = My.Resources.ConexionSGS.schema
        usuario.Text = My.Resources.ConexionSGS.usuario
        base.Text = My.Resources.ConexionSGS.db
    End Sub
    Private Sub ConexionDesdeAPP_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub SimpleButton3_Click(sender As System.Object, e As System.EventArgs) Handles SimpleButton3.Click
        defa()
    End Sub

    Private Sub SimpleButton2_Click(sender As System.Object, e As System.EventArgs) Handles SimpleButton2.Click

        Dim conexion As New Devart.Data.PostgreSql.PgSqlConnection(TextBox7.Text)
        Try
            conexion.Open()
            MsgBox("Prueba de conexión satisfactoria", MsgBoxStyle.Information, "Prueba de conexión")
        Catch ex As Exception
            MsgBox("No se puedo establecer la conexión con la base de datos, para mayor información consulte con el personal técnico", MsgBoxStyle.Critical, "Error con la conexión")

        End Try
    End Sub

    Private Sub SimpleButton1_Click(sender As System.Object, e As System.EventArgs) Handles SimpleButton1.Click


        Dim conexion2 As New Devart.Data.PostgreSql.PgSqlConnection(TextBox7.Text)
        Try
            'Para comprobar si existe una conexion
            conexion2.Open()
            '************************************************

            'Elimina el archivo
            Try
                My.Computer.FileSystem.DeleteFile(My.Resources.ConexionSGS.archivoConfiguaracion)
            Catch ex As Exception
            End Try
            '************************************************

            'Codigo para escribir en el archivo
            My.Settings.CambiaCnxDB = TextBox7.Text

            Dim fic As String = My.Resources.ConexionSGS.archivoConfiguaracion
            Dim diract As String = ""
            diract = System.IO.Directory.GetCurrentDirectory()

            Dim direccion As String = ""

            direccion = String.Format("{0}\{1}", diract, fic)

            Dim host1 As String = host.Text
            Dim usuario1 As String = usuario.Text
            Dim contrasena1 As String = contraseña.Text
            Dim base1 As String = base.Text
            Dim schema1 As String = schema.Text
            Dim sw As New System.IO.StreamWriter(direccion, True)

            sw.WriteLine(String.Format("User Id={0};Password={1};Host={2};Database={3};Persist Security Info=True;Schema={4}", usuario1, contrasena1, host1, base1, schema1))
            sw.Close()
            '************************************************

            MsgBox("El Sistema se reiniciará", MsgBoxStyle.Information, "Aviso")



            Application.Restart()



        Catch ex As Exception
            MsgBox("No se pudo establecer la conexión con la base de datos, para mayor información consulte con el personal técnico", MsgBoxStyle.Critical, "Error con la conexión")
        End Try
    End Sub

    Private Sub SimpleButton4_Click(sender As System.Object, e As System.EventArgs) Handles SimpleButton4.Click

        Me.Close()
    End Sub

    Private Sub host_TextChanged(sender As System.Object, e As System.EventArgs) Handles host.TextChanged
        con()
    End Sub

    Private Sub puerto_TextChanged(sender As System.Object, e As System.EventArgs) Handles puerto.TextChanged
        con()
    End Sub

    Private Sub usuario_TextChanged(sender As System.Object, e As System.EventArgs) Handles usuario.TextChanged
        con()
    End Sub

    Private Sub contraseña_TextChanged(sender As System.Object, e As System.EventArgs) Handles contraseña.TextChanged
        con()
    End Sub

    Private Sub base_TextChanged(sender As System.Object, e As System.EventArgs) Handles base.TextChanged
        con()
    End Sub

    Private Sub schema_TextChanged(sender As System.Object, e As System.EventArgs) Handles schema.TextChanged
        con()
    End Sub
End Class